﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AmkeniCrossLoginApplication.Data_Access;

namespace AmkeniCrossLoginApplication.Amkeni_System.CO
{
    public partial class Process_Indicators : System.Web.UI.Page
    {
        Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void IPEntityDataSource_QueryCreated(object sender, QueryCreatedEventArgs e)
        {
            var indicators = e.Query.Cast<indicator>();

            int ipID = getIP();

            e.Query = (from data in indicators
                      from planData in data.activity_indicator_connector
                      where planData.monitoring_plan.ip_id == ipID
                      select data).Distinct();
            //e.Query = from data in indicators where data.monitoring_plan == plan select data;
        }

        private int getIP()
        {
            int ip = 0;
            var U_IP = (from u in db.users where u.email == User.Identity.Name select new { u.ip_id }).SingleOrDefault();
            if (U_IP != null)
            {
                int ipNum;
                if (int.TryParse(U_IP.ip_id.ToString(), out ipNum))
                {
                    ip = int.Parse(U_IP.ip_id.ToString());
                }
            }
            return ip;
        }
    }
}